<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BAError
 *
 * @author jianfeng
 */
class BAError {

    //put your code here
    protected $messages = array();
    protected $errors = array();
    public $status;
    public $sender;
    public $receiver;

    public function __construct($message = 'error', $status = 'error', $sender = null) {
        if (is_string($message) && $status != NULL) {
            $this->messages[$status] = $message;
        }
        if (is_array($message))
            $this->messages = $message;

        $this->status = $status;

        if ($sender != NULL) {
            $this->sender = $sender;
            $this->errors[$status] = array('sender' => $sender);
            if (isset($this->messages[$status]))
                $this->errors[$status] = array('message' => $this->messages[$status]);
        }
    }

    public function addError($message = null, $status = 'error', $sender = null) {
        if ($status == NULL) {
            $status = $this->status;
        }

        if ($message != NULL) {
            $this->messages[$status] = $message;
            $this->errors[$status]['message'] = $message;
        }

        if ($sender == NULL)
            $sender = $this;

        if ($sender != NULL) {
            $this->errors[$status]['sender'] = $sender;
        }
    }

    public function getError($status = 'error') {
        if ($status != NULL) {
            if (isset($this->errors[$status])) {
                return $this->errors[$status];
            }
        }
        return NULL;
    }

    public function getErrorMessage($status = 'error') {
        if ($status != NULL) {
            if (isset($this->messages[$status])) {
                return $this->messages[$status];
            }
        }
        return NULL;
    }

    public function getErrorMessages() {
        return $this->messages;
    }

    public function getErrorSender($status = 'error') {
        if ($status != NULL) {
            if (isset($this->errors[$status])) {
                if (isset($this->errors[$status]['sender']))
                    return $this->errors[$status]['sender'];
            }
        }
        return NULL;
    }

    public function haveErrors() {
        if ($this->errors != NULL)
            return TRUE;
        return FALSE;
    }

}
